package com.xing.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xing.gulimall.product.entity.BrandEntity;
import com.xing.gulimall.product.entity.CategoryEntity;
import com.xing.gulimall.product.service.BrandService;
import com.xing.gulimall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xing.common.utils.PageUtils;
import com.xing.common.utils.Query;

import com.xing.gulimall.product.dao.CategoryBrandRelationDao;
import com.xing.gulimall.product.entity.CategoryBrandRelationEntity;
import com.xing.gulimall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {


    //spring在应用程序上下文启动时去创建所有单例bean对象，
    // 而@Lazy注解可以延迟加载bean对象，即在使用时才去初始化
    //所以，
    //  @lazy注解，
    //  1.是可以减少Spring的IOC容器启动时的加载时间，
    //  2.是可以解决bean的循环依赖问题
    @Lazy
    @Autowired
    private CategoryService categoryService;

    @Lazy
    @Autowired
    private BrandService brandService;

    @Autowired
    private CategoryBrandRelationDao relationDao;




    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 新增接口和实现类
     * 新增品牌与分类关联关系
     *
     * @param categoryBrandRelation
     */
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();

        //1.查询详细名字
        CategoryEntity categoryEntity = categoryService.getById(catelogId);
        BrandEntity brandEntity = brandService.getById(brandId);
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        this.baseMapper.insert(categoryBrandRelation);

    }

    @Override
    public void updateDetail(CategoryBrandRelationEntity categoryBrandRelationEntity) {
        UpdateWrapper<CategoryBrandRelationEntity> wrapper = new UpdateWrapper<CategoryBrandRelationEntity>()
                .eq("brand_id", categoryBrandRelationEntity.getBrandId())
                .set("brand_name", categoryBrandRelationEntity.getBrandName());
        this.baseMapper.update(categoryBrandRelationEntity, wrapper);
    }


    @Override
    public List<BrandEntity> getBrandList(Long catId) {
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("catelog_id", catId);
        List<CategoryBrandRelationEntity> relationEntityList = this.baseMapper.selectList(wrapper);

        return relationEntityList.stream().map((item) -> {
            Long brandId = item.getBrandId();
            return brandService.getById(brandId);
        }).collect(Collectors.toList());
    }

    /**
     * 新增接口和实现类方法
     * 获取分类关联的品牌
     *
     * @param catId
     * @return
     */
    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {
        List<CategoryBrandRelationEntity> catelogId = relationDao.selectList(
                new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        List<BrandEntity> collect = catelogId.stream().map(item -> {
            Long brandId = item.getBrandId();
            BrandEntity byId = brandService.getById(brandId);
            return byId;
        }).collect(Collectors.toList());
        return collect;
    }
}